﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Templates
{
    public class OverflowSection : ITemplate, IXmlBuildable
    {
        public Enumerations.DisplayMode? DisplayMode { get; set; }
        public bool DividerAfter { get; set; }
        public bool DividerBefore { get; set; }
        public string TemplateAlias { get; set; }
        public Enumerations.SectionType? Type { get; set; }

        public OverflowSection()
        {}

        public OverflowSection(XmlNode node)
        {
            try
            {
                this.DisplayMode = (Enumerations.DisplayMode)Enum.Parse(typeof(Enumerations.DisplayMode), XmlHelper.GetAttribute(node, "DisplayMode"));
                this.DividerAfter = XmlHelper.GetAttribute(node, "DividerAfter") == "true";
                this.DividerBefore = XmlHelper.GetAttribute(node, "DividerBefore") == "true";
                this.TemplateAlias = XmlHelper.GetAttribute(node, "TemplateAlias");
                this.Type = (Enumerations.SectionType)Enum.Parse(typeof(Enumerations.SectionType), XmlHelper.GetAttribute(node, "Type"));
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode layoutNode)
        {
            try
            {
                XmlNode overflowSectionNode = XmlHelper.AddNode(layoutNode, "OverflowSection");
                XmlHelper.AddAttribute(overflowSectionNode, "DisplayMode", this.DisplayMode);
                XmlHelper.AddAttribute(overflowSectionNode, "DividerAfter", this.DividerAfter);
                XmlHelper.AddAttribute(overflowSectionNode, "DividerBefore", this.DividerBefore);
                XmlHelper.AddAttribute(overflowSectionNode, "TemplateAlias", this.TemplateAlias);
                XmlHelper.AddAttribute(overflowSectionNode, "Type", this.Type);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("OverflowSection");
                node.ContextMenuStrip = ContextMenuStripHelper.BuildStandardContextMenuStrip(node);
                node.Tag = this;

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
